﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;

namespace Common.Dao
{
    public class RtnHotListDao
    {
        //保存龙虎榜
        public static void BatchSaveNotices(List<HotList> list)
        {
            String sql =
                @"insert into `rtn_hot_list`(sec_code,sec_name,publish_date,buy_amount,sell_amount,type,direction,type_details,department_name) " +
                "VALUES (@SecCode,@SecName,@Date,@BuyAmount,@SellAmount,@Type,@Direction,@TypeDetails,@DepartmentName)";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql, list);
            }
        }

        //获取最新一条龙虎榜
        public static HotList GetLast()
        {
            string sql =
                "SELECT sec_code AS secCode,sec_name AS secName,publish_date AS Date,buy_amount AS BuyAmount,sell_amount AS SellAmount,type,direction," +
                "type_details AS TypeDetails,department_name AS DepartmentName,create_time AS createtime from `rtn_hot_list`" +
                "ORDER BY publish_date DESC LIMIT 1";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var list = conn.Query<HotList>(sql).ToList();
                return list.Any() ? list.First() : null;
            }
        }
        

        //获取全部龙虎榜
        public static List<HotList> GetAll()
        {
            string sql =
                "SELECT sec_code AS secCode,sec_name AS secName,publish_date AS Date,buy_amount AS BuyAmount,sell_amount AS SellAmount,type,direction," +
                "type_details AS TypeDetails,department_name AS DepartmentName,create_time AS createtime from `rtn_hot_list`" +
                "ORDER BY publish_date DESC ";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<HotList>(sql)?.ToList();
            }
        }

    }
}